package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.ProperFee;
import com.ruoyi.system.domain.SysFloor;
import com.ruoyi.system.mapper.SysProperFeeMapper;
import com.ruoyi.system.service.ISysProperFeeService;
import com.ruoyi.system.service.ISysProperFeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 物管费信息Service业务层处理
 *
 * @author zmz
 * @date 2021-08-22
 */
@Service
public class SysProperFeeServiceImpl implements ISysProperFeeService
{
    @Autowired(required = false)
    private SysProperFeeMapper SysProperFeeMapper;

    /**
     * 查询物管费信息
     *
     * @param properId 物管费信息ID
     * @return 物管费信息
     */
    @Override
    public ProperFee selectProperFeeById(String properId)
    {
        return SysProperFeeMapper.selectProperFeeById(properId);
    }

    /**
     * 查询物管费信息
     *
     * @param ProperFeeName 物管费信息ID
     * @return 物管费信息
     */
    @Override
    public ProperFee selectProperFeeByName(String ProperFeeName)
    {
        return SysProperFeeMapper.selectProperFeeByName(ProperFeeName);
    }

    /**
     * 查询物管费信息
     *
     * @param etldt 物管费信息ID
     * @return 物管费信息
     */
    @Override
    public List<ProperFee> selectProperFeeListByEtldt(String etldt)
    {
        return SysProperFeeMapper.selectProperFeeListByEtldt(etldt);
    }

    /**
     * 查询物管费信息列表
     *
     * @param properFee 物管费信息
     * @return 物管费信息
     */
    @Override
    public List<ProperFee> selectProperFeeList(ProperFee properFee)
    {
        return SysProperFeeMapper.selectProperFeeList(properFee);
    }

    /**
     * 查询物管费信息列表
     *
     * @param properFee 物管费信息
     * @return 物管费信息
     */
    @Override
    public List<ProperFee> selectProperFeeListAll(ProperFee properFee)
    {
        return SysProperFeeMapper.selectProperFeeListAll(properFee);
    }



    /**
     * 物管费基础信息
     *
     * @param properFee 物管费信息
     * @return 结果
     */
    @Override
    public int insertProperFee(ProperFee properFee)
    {
        return SysProperFeeMapper.insertProperFee(properFee);
    }

    /**
     * 修改物管费信息
     *
     * @param properFee 物管费信息
     * @return 结果
     */
    @Override
    public int updateProperFee(ProperFee properFee)
    {
        return SysProperFeeMapper.updateProperFee(properFee);
    }

    /**
     * 删除物管费信息对象
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteProperFeeByIds(String[] ids)
    {
        return SysProperFeeMapper.deleteProperFeeByIds(ids);
    }

    /**
     * 根据日期批次删除物管费信息
     *
     * @param etldt 需要删除的数据的日期批次
     * @return 结果
     */
    @Override
    public int deleteProperFeeByEtldt(String etldt) {
        return SysProperFeeMapper.deleteProperFeeByEtldt(etldt);
    }

    /**
     * 查询物管费信息列表
     *
     * @param properIds 物管费信息
     * @return 物管费信息集合
     */
    @Override
    public List<ProperFee> selectProperFeeListByIds(String[] properIds){
        return SysProperFeeMapper.selectProperFeeListByIds(properIds);
    }

    /**
     * 批量插入物管费
     * @param properFeeList
     * @return
     */
    @Override
    public int insertProperFees(List<ProperFee> properFeeList){
        return SysProperFeeMapper.insertProperFees(properFeeList);
    }

    /**
     * 导入物管费
     * @param lists
     * @param updateSupport
     * @param sysUser
     * @return
     */
    @Override
    public String importProperFee(List<ProperFee> lists, boolean updateSupport, SysUser sysUser) {
        int successNum = 0;
        int failureNum = 0;
        StringBuffer successMsg = new StringBuffer();
        StringBuffer failureMsg = new StringBuffer();
        for (ProperFee sb:lists){
            sb.setProperId(UUID.randomUUID().toString().replace("-",""));
            sb.setUpdatetime(new Date());
            try {
                //验证是否存在这个物管费
                ProperFee properFee = SysProperFeeMapper.selectProperFeeByEtldt(sb.getEtldt(),sb.getCompanyName(),sb.getFloorName(),sb.getCoveredArea());
                if (StringUtils.isNull(properFee)){
                    if(!sb.getFloorName().equals("合计")){
                        SysProperFeeMapper.insertProperFee(sb);
                        successNum++;
                        successMsg.append("<br/>"+successNum+"、"+sb.getCompanyName()+"的"+sb.getEtldt()+"物管费导入成功");
                    }
                }else if (updateSupport){
                    SysProperFeeMapper.updateProperFee(sb);
                    successNum++;
                    successMsg.append("<br/>"+successNum+"、"+sb.getCompanyName()+"的"+sb.getEtldt()+"物管费更新成功");
                }else{
                    failureNum++;
                    failureMsg.append("<br/>"+failureNum+"、"+sb.getCompanyName()+"的"+sb.getEtldt()+"物管费已存在");
                }
            }catch (Exception e){
                failureNum ++;
                failureMsg.append("<br/>"+failureNum+"、导入失败");
            }
        }
        if(failureNum > 0){
            failureMsg.insert(0, "很抱歉，导入失败！共"+failureNum+"条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }else {
            successMsg.insert(0, "恭喜您数据已全部导入成功！共"+ successNum +"条，数据如下：");
        }
        return successMsg.toString();
    }
}
